<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px"   >
       <el-form-item label="预算编号" prop="name">
         <el-input
           v-model="queryParams.num"
           placeholder="请输入预算编号"
           clearable
           size="small"
           style="width: 146px"
           @keyup.enter.native="handleQuery"
         />
       </el-form-item>
       <el-form-item label="预算名称" prop="name">
         <el-input
           v-model="queryParams.name"
           placeholder="请输入预算名称"
           clearable
           size="small"
           style="width: 146px"
           @keyup.enter.native="handleQuery"
         />
       </el-form-item>
       <el-form-item label="预算年度" prop="year">
         <el-date-picker
           style="width: 146px"
           v-model="queryParams.year"
           type="year"
           placeholder="选择预算年度">
         </el-date-picker>
       </el-form-item>
       <el-form-item label="审核状态" prop="status">
         <el-select
           v-model="queryParams.status"
           clearable
           placeholder="请选择审核状态"
           style="width: 146px"
         >
           <el-option
             v-for="dict in statusOptions"
             :key="dict.dictValue"
             :label="dict.dictLabel"
             :value="dict.dictValue"
           />
         </el-select>
       </el-form-item>
       <el-form-item>
         <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
         <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
       </el-form-item>
     </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['distribution:account:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['distribution:account:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div class="selectedDv" v-if="ids.length > 0">已选<span style="color:#1890FF;padding:  0 6px;">{{ ids.length }}</span>项
    </div>
    <el-table v-loading="loading" :data="accountList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="名称" align="center" prop="frAccName"/>
      <el-table-column label="账号" align="center" prop="frAccNo"/>
      <!--<el-table-column label="账号" align="center" prop="frAccNo">
        <template slot-scope="scope">
          <span>{{ scope.row.frAccNo.replace(/\s/g, '').replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['distribution:account:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['distribution:account:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <div style="min-height: 400px">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="付款户名" prop="frAccName">
            <el-input v-model="form.frAccName" placeholder="请输入付款户名"/>
          </el-form-item>
          <el-form-item label="付款账号" prop="frAccNo">
<!--            <el-input v-model="form.frAccNo" placeholder="请输入付款账号" @input="formatCardNumber" ref="cardInput"/>-->
            <el-input v-model="form.frAccNo" placeholder="请输入付款账号"/>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import {exportBudgetFunds} from "@/api/zjgl/budgetFunds";
import {addAccount, delAccount, getAccount, listAccount, updateAccount} from "@/api/zjgl/account";
import Template from "@/views/message/template/index";

export default {
  name: "BudgetFunds",
  components: {Template},
  data() {
    const bankAccountValid = (rule, value, callback) => {
      value = value.replace(/\s*/g, '');
      let aa=value.substring(0, 2);
      const strBin = '10,13,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99'
      if (!value) {
        return callback(new Error('银行卡号不符合规范'));
      } else if (!Number.isInteger(+value)) {
        callback(new Error('银行卡号不符合规范'));
      } else if (value.trim().length < 12 || value.trim().length > 20) {
        // callback(new Error('银行卡号长度必须在12到19之间'))
        callback(new Error('银行卡号不符合规范'));
      } else if (strBin.indexOf(value.substring(0, 2)) === -1) {
        callback(new Error('银行卡号不符合规范'));
      } else {
        callback()
      }
    }
    return {
      openCheck: false,
      openBudget: false,
      accountList: [],
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      querypgParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        departmentCode: null,
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        budgetId: null,
        name: null,
        year: null,
        sdate: null,
        edate: null,
        budgetpersonid: null,
        budgetperson: null,
        budgetmoney: null,
        submittime: null,
        number: null,
        remarks: null,
        status: null,
        approvedperson: null,
        approveddate: null,
        createtime: null,
        by1: null,
        by2: null,
        by3: null
      },
      value1: [],
      value2: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        frAccName: [
          {required: true, message: "付款户名不能为空", trigger: "blur"}
        ],
        frAccNo: [
          // {required: true, message: "付款账号不能为空", trigger: "blur"}, {validator: bankAccountValid, trigger: 'blur'}
          {required: true, message: "付款账号不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getDicts('sys_normal_disable').then(response => {
      this.statusOptions = response.data
    })
    this.getDicts('sys_budget_status').then(response => {
      this.budgetStatus = response.data
    })
    this.getList();
  },
  methods: {
    formatCardNumber(cardNum) {
      // 获取input的dom对象，element ui的el-input这样才能拿到
      const input = this.$refs.cardInput.$el.getElementsByTagName('input')[0]
      const cursorIndex = input.selectionStart
      const lineNumOfCursorLeft = (cardNum.slice(0, cursorIndex).match(/\s/g) || []).length
      const noLine = cardNum.replace(/\s/g, '')
      const newCardNum = noLine.replace(/\D+/g, '').replace(/(\d{4})/g, '$1 ').replace(/\s$/, '')
      const newLineNumOfCursorLeft = (newCardNum.slice(0, cursorIndex).match(/\s/g) || []).length
      const newCursorIndex = cursorIndex + newLineNumOfCursorLeft - lineNumOfCursorLeft
      this.$nextTick(() => {
        this.form.frAccNo = newCardNum;
        this.$nextTick(() => {
          input.selectionStart = newCursorIndex
          input.selectionEnd = newCursorIndex
        })
      })
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listAccount(this.queryParams).then(response => {
        this.accountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.active = 0
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        frAccName: null,
        frAccNo: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.budgetId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.openYsmx = true
      this.reset();
      const id = row.accountId || this.ids
      this.form.accountId = id;
      getAccount(id).then(response => {
        this.form = response.data;
        // this.form.frAccNo = response.data.frAccNo.replace(/\s/g, '').replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ');
        this.form.frAccNo = response.data.frAccNo;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.accountId != null) {
            this.form.frAccNo = this.form.frAccNo.replace(/\s*/g, '');
            updateAccount(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.departmentCode = "";
            this.form.frAccNo = this.form.frAccNo.replace(/\s*/g, '');
            addAccount(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.openShjl = true
      const ids = row.accountId || this.ids;
      this.$confirm('是否确认删除数据项?', "警告", {
        cancelButtonClass: 'btn-custom-cancel',
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delAccount(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    handleCheck() {
      this.openCheck = true
    },
    handleBudget() {
      this.openBudget = true
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有【请填写功能名称】数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportBudgetFunds(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
<style>
.btn-custom-cancel {
  float: right;
  margin-left: 10px;
}
.selectedDv {
  background-color: rgba(230, 247, 255, 1);
  box-sizing: border-box;
  border-width: 1px;
  border-style: solid;
  border-color: rgba(186, 231, 255, 1);
  border-radius: 4px;
  padding: 10px;
  margin-bottom: 10px;
}

.statusDv {
  display: flex;
  justify-content: center;
  align-items: center;
}

.statudPoint {
  border-radius: 50px;
  width: 6px;
  height: 6px;
  margin-right: 6px;
}

/*审核通过背景色*/
.Bg2 {
  background-color: #13ce66;
}

/*未提交*/
.Bg0 {
  background-color: #1890ff;
}

/*待审核*/
.Bg1 {
  background-color: orange;
}

/*审核失败*/
.Bg3 {
  background-color: red;
}
</style>
